package com.spike.controller;

import com.spike.common.R;
import com.spike.model.BorrowRecord;
import com.spike.model.BorrowRecordSummary;
import com.spike.service.BorrowService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@AllArgsConstructor
public class BorrowController {
    private final BorrowService borrowService;
    @PostMapping("borrow")
    public R borrow(@RequestBody BorrowRecord borrowRecord){
        Integer id = borrowService.borrow(borrowRecord);
        if(id != null){
            HashMap<String,Integer> map = new HashMap<>();
            map.put("recordId",id);
            return new R(200,"借阅成功并已发送短信通知",map);
        }else
            return new R(500,"借阅失败",null);

    }
    @PostMapping("return")
    public R returnBook(@RequestBody BorrowRecord borrowRecord){
        Boolean b = borrowService.retuenBook(borrowRecord);
        if(b)
            return new R(200,"归还成功",null);
        else
            return new R(500,"归还失败",null);
    }
    @GetMapping("borrow/history/{user_id}")
    public R borrowHistory(@PathVariable("user_id") Integer userId){
        List<BorrowRecordSummary> list = borrowService.findListByUserId(userId);
        return new R(200,"获取成功",list);
    }
}
